﻿using System;
using System.Collections.Generic;
using BCCommon;
using BCCommon.NPinyin;
using BCCommon.OAEnums;
using BCEntity.OA.BusinessTypeRecord;
using BCData.OA.BusinessTypeRecord;
using BCDto.OA.BusinessTypeRecord;
using BCService.Utils;

namespace BCService.OA.BusinessTypeRecord
{
    /// <summary>
    /// OA业务类型
    /// </summary>
    public class BusinessTypeRecordService : IBusinessTypeRecordService
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly IBusinessTypeRecordData businessTypeRecordData;

        public BusinessTypeRecordService(IApplicationContextService applicationContextService,
            IBusinessTypeRecordData businessTypeRecordData)
        {
            this.applicationContextService = applicationContextService;
            this.businessTypeRecordData = businessTypeRecordData;
        }

        /// <summary>
        /// 新创建一个OA业务类型
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        public BusinessTypeRecordDto Create(BusinessTypeRecordRequestDto requestDto)
        {
            if (requestDto.AllowOperator == OAAllowOperator.Department)
            {
                if (requestDto.DepartmentFunction.IsNull())
                {
                    throw new ArgumentException("部门信息不能为空。");
                }
            }
            //if (sobSettingData.GetEntityById(requestDto.SobSettingId).GetAwaiter().GetResult() == null)
            //{
            //    throw new ArgumentException("账套信息不存在。");
            //}
            //if (cashierTransportAccountCertificateSettingData.GetEntity(requestDto.SobSettingId, requestDto.CashierTransportAccountCertificateSettingId).GetAwaiter().GetResult() == null)
            //{
            //    throw new ArgumentException("出纳日记账转换记账凭证设置信息不存在。");
            //}
            if (businessTypeRecordData.CheckIsExists(requestDto.TypeName, requestDto.SobSettingId))
            {
                throw new ArgumentException($"已存在类型名称为:{requestDto.TypeName}的类型，不能重复新增。");
            }
            var entity = requestDto.As<BusinessTypeRecordEntity>();
            entity.TypeCode = Pinyin.GetPinyin(requestDto.TypeName);
            return businessTypeRecordData.InsertAsync(entity).GetAwaiter().GetResult().As<BusinessTypeRecordDto>();
        }

        /// <summary>
        /// 修改一个OA业务类型
        /// </summary>
        /// <param name="businessTypeRecordId"></param>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        public BusinessTypeRecordDto Update(long businessTypeRecordId, BusinessTypeRecordRequestDto requestDto)
        {
            if (requestDto.AllowOperator == OAAllowOperator.Department)
            {
                if (requestDto.DepartmentFunction.IsNull())
                {
                    throw new ArgumentException("部门信息不能为空。");
                }
            }
            //if (sobSettingData.GetEntity(requestDto.SobSettingId).GetAwaiter().GetResult() == null)
            //{
            //    throw new ArgumentException("账套信息不存在。");
            //}
            if (!businessTypeRecordData.CheckIsExists(businessTypeRecordId))
            {
                throw new ArgumentException("业务类型记录信息不存在。");
            }
            //if (cashierTransportAccountCertificateSettingData.GetEntity(requestDto.SobSettingId, requestDto.CashierTransportAccountCertificateSettingId).GetAwaiter().GetResult() == null)
            //{
            //    throw new ArgumentException("出纳日记账转换记账凭证设置信息不存在。");
            //}
            var entity = requestDto.As<BusinessTypeRecordEntity>();
            entity.BusinessTypeRecordId = businessTypeRecordId;
            entity.TypeCode = Pinyin.GetPinyin(requestDto.TypeName);
            return businessTypeRecordData.UpdateAsync(entity).GetAwaiter().GetResult().As<BusinessTypeRecordDto>();
        }

        /// <summary>
        /// 分页获取OA业务类型记录(列表)
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="start">创建时间开始</param>
        /// <param name="end">创建时间结束</param>
        /// <param name="ascending">是否升序排序</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <returns></returns>
        public PageableList<BusinessTypeRecordDto> GetList(string typeName, DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            var (datas, dataCout) = this.businessTypeRecordData.GetListAsync(typeName, start, end, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
            return new PageableList<BusinessTypeRecordDto>
            {
                Count = dataCout,
                Items = datas.As<List<BusinessTypeRecordDto>>()
            };
        }

    }
}
